function StationNameEdit(filena)
%StationNameEdit - station name editing in a RINEX header file
%
%           StationRinexEdit(filena)
%
% This function changes, if necessary, the MARKER NAME of the header of 
% the 2.11 RINEX file whose name is FILENA. It is an ancillary function
% for the RINEX3 -> RINEX2 conversion.
%
%  See also Rinex32Rinex2, Rinex32Rinex2single. 

% G. Teza, 2022

[~,filenar,~] = fileparts(filena);

statName = filenar(1:4);
% Read txt into cell A
fid = fopen(filena,'r');
i = 1;
tline = fgetl(fid);
A{i} = tline;
while ischar(tline)
    i = i+1;
    tline = fgetl(fid);
    A{i} = tline;
end
fclose(fid);

% Row search
for k=1:10
    Ak = A{k};
    if strcmpi(statName,Ak(1:4))
        ks = k;
        if strcmpi(Ak(5),' ')   % in this case the change name is unnecessary
            return
        end
    end
end

% Change detected row of cell A
Aks = A{ks};
Aks(5:9) = '     ';
A(ks) = {Aks};

% Write cell A into txt
fid = fopen(filena, 'w+');
for i = 1:numel(A)
    if A{i+1} == -1
        fprintf(fid,'%s', A{i});
        break
    else
        fprintf(fid,'%s\n', A{i});
    end
end

fclose(fid);